package com.meituan.controller;

import com.alibaba.fastjson.JSONObject;
import com.meituan.model.GoodsEntity;
import com.meituan.model.OrderEntity;
import com.meituan.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@RestController
public class OrderController {
    @Autowired
    OrderService orderService;

    @PostMapping("placeOrder")
    public Map placeOrder(@RequestBody Map<String,Object > data){
        Map map = new HashMap();
        List<GoodsEntity> list = JSONObject.parseArray(JSONObject.toJSONString(data.get("goodsList"))).toJavaList(GoodsEntity.class);
        OrderEntity orderEntity = JSONObject.parseObject(JSONObject.toJSONString(data.get("order"))).toJavaObject(OrderEntity.class);
        Integer result = orderService.placeOrder(orderEntity,list);
        if(result>0){
            map.put("err_code",0);
            map.put("result",result);
        }else{
            map.put("err_code",1);
        }
        return map;
    }

    @GetMapping("getOrderByUserId")
    public Map getOrderByUserId(String userId){
        Map map = new HashMap();
        List<OrderEntity> list = orderService.findAllByUserId(userId);
        if(list.size()!=0){
            map.put("err_code",0);
            map.put("result",list);
        }else{
            map.put("err_code",1);
        }
        return map;
    }
    @GetMapping("getOrderByShopId")
    public Map getOrderByShopId(String shopId){
        Map map = new HashMap();
        List<OrderEntity> list = orderService.findAllByShopId(shopId);
        if(list.size()!=0){
            map.put("err_code",0);
            map.put("result",list);
        }else{
            map.put("err_code",1);
        }
        return map;
    }

    @GetMapping("getOrderById")
    public OrderEntity getOrderById(String orderId){
        return orderService.findById(orderId);
    }

    @GetMapping("doPay")
    public Integer doPay(String orderId){
        Integer result = orderService.updateIsPay( orderId);
        return result;
    }

    @GetMapping("doOver")
    public Integer doOver(String orderId){
        Integer result = orderService.updateIsOver( orderId);
        return result;
    }

    @GetMapping("delOrder")
    public Integer delOrder(String orderId){
        Integer result = orderService.updateIsDelete( orderId);
        return result;
    }

}
